#include "popen_wrapper.h"

POpen::POpen(const std::string arg):
state(STATE_CLOSED)
{
	open(arg.c_str());
}

POpen::POpen(const char* arg):
state(STATE_CLOSED)
{
	open(arg);
}

POpen::~POpen()
{
	if(fd != NULL)
		pclose(fd);
}

std::string POpen::GetData()
{
	update();
	return data;
}

void POpen::open(const char* arg)
{
	fd = popen(arg, "r");
	if(fd == NULL)
	{
		state = STATE_ERROR;
		return;
	}
	state = STATE_OPEN;
}

void POpen::update()
{
	if(state == STATE_OPEN)
	{
		char input[256];
		while(fgets(input, 256, fd) != NULL)
		{
			data += input;
		}
	}
	if(feof(fd) != 0)
		state = STATE_EOF;
}
